package com.telit.firecommand.controller;

import com.telit.common.entity.DataReturn;
import com.telit.communicate.dto.MsgInfoDTO;
import com.telit.communicate.dto.MsgProcessPathDTO;
import com.telit.communicate.dto.UpAndDownLineDTO;
import com.telit.communicate.feign.RemoteCommunicateService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zhouyu
 * @description web端websocket消息处理类
 */
@RestController
@Api(tags = "web端websocket消息处理类")
public class FireCommandMessageProcessController implements ApplicationRunner {

//    @Autowired
//    private FireQueryService fireQueryService;
//
//    @Autowired
//    private EventService eventService;
//
//    @Autowired
//    private TaskFeedBackService taskFeedBackService;

    @Autowired
    private RemoteCommunicateService communicateFeignClient;

//    @Autowired
//    private CommunicateRecordService communicateRecordService;

//    @Autowired
//    private AliVideoService aliVideoService;
//
//    @Autowired
//    private UpmsFeignClient upmsFeignClient;


    @Override
    public void run(ApplicationArguments args) {
        //单独启动线程，防止项目启动失败
        new Thread(() -> {
            MsgProcessPathDTO msgProcessPathDTO = new MsgProcessPathDTO();
            msgProcessPathDTO.setSystemName("FIREPROOF");
            msgProcessPathDTO.setMsgSource("WEB");
            msgProcessPathDTO.setMessageProcessPath("http://yichang-firecommand/firecommand/messageProcess");
            msgProcessPathDTO.setUpAndDownProcessPath("http://yichang-firecommand/firecommand/upAndDownProcess");
            communicateFeignClient.subscribeMsgProcess(msgProcessPathDTO);
        }).start();
        //服务异常结束 回调方法取消订阅
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            MsgProcessPathDTO msgProcessPathDTO = new MsgProcessPathDTO();
            msgProcessPathDTO.setSystemName("FIREPROOF");
            msgProcessPathDTO.setMsgSource("WEB");
            communicateFeignClient.unSubscribeMsgProcess(msgProcessPathDTO);
        }));
    }


    @PostMapping(value = "/messageProcess", produces = "application/json;charset=UTF-8")
    public DataReturn messageProcess(@RequestBody MsgInfoDTO msgInfoDTO) {
        return DataReturn.ok();
    }

    @PostMapping(value = "/upAndDownProcess", produces = "application/json;charset=UTF-8")
    DataReturn upAndDownProcess(@RequestBody UpAndDownLineDTO upAndDownLineDTO) {
//        long userId = Long.parseLong(upAndDownLineDTO.getUserId());
//        Result<List<SysRole>> sysRoleResult = upmsFeignClient.queryRoleByUserId(userId);
//        //当websocket重连的时候，角色是“防火管理员”才推送
//        if (upAndDownLineDTO.getUpAndDown()==0  ){
//            if(ObjectUtil.isNotEmpty(sysRoleResult.getData())) {
//                for (SysRole sysRole:sysRoleResult.getData()){
//                    if("NJ_FHGLY".equals(sysRole.getRoleCode())){
//                        // 1. 推送本级未读或未接警完成火情
//                        boolean hasUnReadCurrentLevelFire = fireQueryService.queryHasUnReadCurrentLevelFire(userId);
//                        SendMessageDTO sendMessageDTO = new SendMessageDTO();
//                        sendMessageDTO.setFlag("special");
//                        String[] userIds = {String.valueOf(userId)};
//                        sendMessageDTO.setUserId(userIds);
//                        if (hasUnReadCurrentLevelFire) {
//                            sendMessageDTO.setMsg(JSON.toJSONString(DataReturn.ok(MsgIdentificationConstants.FIRE_ALARM, null)));
//                            communicateFeignClient.sendMsg(sendMessageDTO);
//                        }
//                        //3.推送用户未读事件
//                        boolean hasUnReadEvent = eventService.queryHasUnReadEvent(userId);
//                        if (hasUnReadEvent) {
//                            sendMessageDTO.setMsg(JSON.toJSONString(DataReturn.ok(MsgIdentificationConstants.REPORT_EVENT, null)));
//                            communicateFeignClient.sendMsg(sendMessageDTO);
//                        }
//                        //4.推送火情核实任务反馈
//                        Boolean hasUnReadTaskFeedBack = taskFeedBackService.queryHasUnReadTaskFeedBack(userId);
//                        if (hasUnReadTaskFeedBack) {
//                            sendMessageDTO.setMsg(JSON.toJSONString(DataReturn.ok(MsgIdentificationConstants.FIRE_VERIFICATION_FEEDBACK, null)));
//                            communicateFeignClient.sendMsg(sendMessageDTO);
//                        }
//                    }
//                }
//
//            }
//        }else{
//            // 融合通信断开,退出房间
//            CommCommunicateRecord record = communicateRecordService.listBySponsor(userId);
//            if(record != null){
//                // 1.若是房主更新融合通信记录,结束时间,通话时长
//                communicateRecordService.finishMeeting(record);
//
//                // 2.1如果userId是创建房间的房主,那么就通知所有的参会人员结束通话
//                aliVideoService.pushMsg(record);
//            }else{
//                // todo 不是房主是否需要推送消息还需讨论
//                // 2.2若不是房主,就通知web端哪个人离开房间
//                aliVideoService.pushWebMsg(userId);
//            }
//        }
        return DataReturn.ok();
    }

}
